//
//  loginViewController.m
//  FingerUniversity
//
//  Created by vantle on 16/3/11.
//  Copyright © 2016年 vantle. All rights reserved.
//

#import "loginViewController.h"
#import "SVProgressHUD.h"
#import "MyMD5.h"
#import "AFNetworking.h"
#import "HomeViewController.h"
#import "UIColor+hexColor.h"
#import "MobClick.h"
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
@interface loginViewController ()

@end

@implementation loginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString*loginName;
    NSString*passWord;
    self.navigationController.navigationBarHidden=YES;
    self.view.backgroundColor=[UIColor whiteColor];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    loginName=[userDefaults objectForKey:@"loginName"];
    passWord=[userDefaults objectForKey:@"passWord"];

    if (loginName.length==0||passWord.length==0) {
        [self setContent];
    }
    else
    {
        NSString*passWordStr= [MyMD5 md5:passWord];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary*params=@{@"userName":loginName,@"passWord":passWordStr};
        [manager POST:@"http://120.76.79.85:9080/mzdx/service/login.action" parameters:params success: ^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSString*rType=[responseObject objectForKey:@"rType"];
            NSString*userId=[responseObject objectForKey:@"userId"];
            NSUserDefaults *userIdDefaults = [NSUserDefaults standardUserDefaults];
            [userIdDefaults setObject:userId forKey:@"userId"];
            if ([rType isEqualToString:@"1"]) {
                
                HomeViewController*homeVC=[[HomeViewController alloc]init];
                [self.navigationController pushViewController:homeVC animated:YES];
                //统计开启
                [MobClick profileSignInWithPUID:userId];
            }
            else
            {
                [self setContent];
            }
        } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
            [self setContent];
        }];
    }
    
}
-(void)setContent
{
    UIImageView*bg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
    bg.image=[UIImage imageNamed:@"bg"];
    bg.userInteractionEnabled=YES;
    [self.view addSubview:bg];
    
    UIImageView*logoImage=[[UIImageView alloc]init];
    logoImage.center=CGPointMake(self.view.center.x, 100);
    logoImage.bounds=CGRectMake(0, 0, 50, 50);
    logoImage.image=[UIImage imageNamed:@"logo3"];
    [bg addSubview:logoImage];
    
//    UIImageView*loginImage=[[UIImageView alloc]init];
//    loginImage.center=CGPointMake(ScreenWidth/2, 200);
//    loginImage.image=[UIImage imageNamed:@"txt"];
//    loginImage.bounds=CGRectMake(0, 0, ScreenWidth-54, 50);
//    [self.view addSubview:loginImage];
//    
//    UIImageView*passsWordImage=[[UIImageView alloc]init];
//    passsWordImage.center=CGPointMake(ScreenWidth/2, 270);
//    passsWordImage.image=[UIImage imageNamed:@"txt"];
//    passsWordImage.bounds=CGRectMake(0, 0, ScreenWidth-54, 50);
//    [self.view addSubview:passsWordImage];
    
    UIImageView*loginImage=[[UIImageView alloc]init];
    loginImage.center=CGPointMake(ScreenWidth/2, 225);
    loginImage.bounds=CGRectMake(0, 0, ScreenWidth-54, 100);
    loginImage.backgroundColor=[UIColor hexFloatColor:@"eef3f4"];
    loginImage.layer.cornerRadius=6;
    loginImage.clipsToBounds=YES;
    [self.view addSubview:loginImage];
    
    UIImageView*lineView=[[UIImageView alloc]init];
    lineView.backgroundColor=[UIColor hexFloatColor:@"d9d9d9"];
    lineView.center=CGPointMake(ScreenWidth/2, 225);
    lineView.bounds=CGRectMake(0, 0, ScreenWidth-64, 1);
    [self.view addSubview:lineView];
    
    _loginName=[[UITextField alloc]init];
    _loginName.center=CGPointMake(ScreenWidth/2, 200);
    _loginName.placeholder=@"  请输入用户名";
    _loginName.delegate=self;
    _loginName.textColor=[UIColor blackColor];
    _loginName.bounds=CGRectMake(0, 0, ScreenWidth-60, 50);
    [self.view addSubview:_loginName];
    
    UIImageView*touImageView=[[UIImageView alloc]init];
    touImageView.center=CGPointMake(ScreenWidth-60, 200);
    touImageView.bounds=CGRectMake(0, 0, 20, 21);
    touImageView.image=[UIImage imageNamed:@"iconfont-yonghu"];
    [self.view addSubview:touImageView];
    
    _passWord=[[UITextField alloc]init];
    _passWord.center=CGPointMake(ScreenWidth/2, 250);
    _passWord.delegate=self;
    _passWord.textColor=[UIColor blackColor];
    _passWord.secureTextEntry=YES;
    _passWord.placeholder=@"  请输入密码";
    _passWord.bounds=CGRectMake(0,0,ScreenWidth-60, 50);
    [self.view addSubview:_passWord];
    
    UIImageView*souImage=[[UIImageView alloc]init];
    souImage.center=CGPointMake(ScreenWidth-60, 250);
    souImage.bounds=CGRectMake(0, 0, 17, 20);
    souImage.image=[UIImage imageNamed:@"iconfont-mima"];
    [self.view addSubview:souImage];
    
    _loginFree=[UIButton buttonWithType:UIButtonTypeCustom];
    _loginFree.frame=CGRectMake(40, 270, 20, 20);
    [_loginFree setBackgroundImage:[UIImage imageNamed:@"圆角矩形-31"] forState:UIControlStateNormal];
    _loginFree.tag=10086;
    [_loginFree addTarget:self action:@selector(loginFreeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [bg addSubview:_loginFree];
    
    
    UILabel*loginLabel=[[UILabel alloc]initWithFrame:CGRectMake(_loginFree.frame.origin.x+_loginFree.frame.size.width+10, _loginFree.frame.origin.y, 100, 20)];
    loginLabel.text=@"自动登录";
    loginLabel.textColor=[UIColor hexFloatColor:@"d9dcdb"];
    [bg addSubview:loginLabel];
    
    
    UIButton*button=[UIButton buttonWithType: UIButtonTypeCustom];
    button.center=CGPointMake(ScreenWidth/2, loginLabel.frame.origin.y+loginLabel.frame.size.height+50);
    button.bounds=CGRectMake(0, 0, ScreenWidth-54, 50);
    button.layer.cornerRadius=6;
    button.clipsToBounds=YES;
    button.titleLabel.font=[UIFont systemFontOfSize:20];
    [button setBackgroundImage:[UIImage imageNamed:@"圆角矩形-2"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor hexFloatColor:@"ffffff"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"登录" forState:UIControlStateNormal];
    [bg addSubview:button];
    
    
}
-(void)loginFreeButtonClick:(UIButton*)sender
{
    if(sender.tag==10086)
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"勾选"] forState:UIControlStateNormal];
        sender.tag=10010;
    }
    else
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"圆角矩形-31"] forState:UIControlStateNormal];
        sender.tag=10086;
    }
    NSLog(@"=====%d",(int)sender.tag);
}
-(void)buttonClick
{
    
        if (_loginName.text.length==0) {
            [SVProgressHUD showImage:nil status:@"请输入用户名"];
        }
        else if (_passWord.text.length==0)
        {
            [SVProgressHUD showImage:nil status:@"请输入密码"];
        }
        else
        {
            NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
            NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
          NSData*data= [NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil];
            
            if (data == nil) {
                
                [SVProgressHUD showImage:nil status:@"你已经断开连接,请检查你的网络连接然后再次尝试"];
            }
            else{
                [self getNoticeInfo];
            }
            
        }
    
    
}

-(void)getNoticeInfo
{
    [SVProgressHUD show];
    NSString*str= [MyMD5 md5:_passWord.text];
    NSLog(@"=====%@",str);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes =[manager.responseSerializer.acceptableContentTypes setByAddingObject: @"text/html"];
    
    NSDictionary*params=@{@"userName":_loginName.text,@"passWord":str};
    
    [manager POST:@"http://120.76.79.85:9080/mzdx/service/login.action" parameters:params success: ^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [SVProgressHUD dismiss];
        NSLog(@"~~~~~~~~~~%@",responseObject);
        NSString*rType=[responseObject objectForKey:@"rType"];
        
        
        if ([rType isEqualToString:@"1"]) {
            
            NSString*realName=[responseObject objectForKey:@"realName"];
            NSString*userId=[responseObject objectForKey:@"userId"];
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

            
            
            NSString*str= [MyMD5 md5:userId];
            NSString*flag1= [str substringWithRange:NSMakeRange(2, 7)];
            NSString*flag2=[str substringWithRange:NSMakeRange(15, 7)];
            NSString*flag=[NSString stringWithFormat:@"%@%@",flag1,flag2];
            
            [userDefaults setObject:userId forKey:@"userId"];
            [userDefaults setObject:flag forKey:@"flag"];
            [userDefaults setObject:realName forKey:@"realName"];
            
            if (_loginFree.tag==10010) {
                
                [userDefaults setObject:_loginName.text forKey:@"loginName"];
                [userDefaults setObject:_passWord.text forKey:@"passWord"];
            }
            
            HomeViewController*homeVC=[[HomeViewController alloc]init];
            [self.navigationController pushViewController:homeVC animated:YES];
            //统计开启
            [MobClick profileSignInWithPUID:userId];
            
        }
        else
        {
            [SVProgressHUD showImage:nil status:@"账号或密码错误"];
        }
    } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showImage:nil status:@"账号或密码错误"];
        NSLog(@"%@",error);
    }];
    
}




- (void)animationFinished:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    
    if ([animationID compare:@"exitApplication"] == 0) {
        
        exit(0);
        
    }
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.view endEditing:YES];
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
